package com.rightsidetech.http.aspect;

import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.exception.BizException;
import com.rightsidetech.commom.utils.StringUtil;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreInfoService;
import com.rightsidetech.system.service.SysUserService;
import com.rightsidetech.user.entity.UsUserLoginInfo;
import com.rightsidetech.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author huanglt
 * @version 2.0
 * @Title:
 * @Package
 * @Description: 描述
 * @date 2021/4/17
 */
@Slf4j
@Aspect
@Component
public class TokenAspect {
    @Resource
    private HttpServletRequest request;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private SysStoreInfoService sysStoreInfoService;


    @Before("execution(* com.rightsidetech.*.controller..*.*(..)) " +
            "&& @annotation(com.rightsidetech.http.aspect.Token)")
    public void doAround(JoinPoint pjp) throws Exception {
        checkToken(pjp);
    }
    private void checkToken(JoinPoint pjp) throws Exception {
        String uri = this.request.getRequestURI();
        if (StringUtil.isNullorEmpty(uri)) {
            throw new BizException(CodeIdEnum.Expired);
        }
        String[] uriarray = uri.split("/");
        if (uriarray.length < 2) {
            throw new BizException(CodeIdEnum.Expired);
        }

        String tokenAdmin = this.request.getHeader("token_admin");
        String tokenApp = this.request.getHeader("token_userApp");
        String tokenAgentAdmin = this.request.getHeader("token_agentAdmin");
        String tokenStoreApp = this.request.getHeader("token_storeApp");
        String tokenAgentApp = this.request.getHeader("token_agentApp");
        //进行token校验
        //总平台-loginInfoId
        if (!StringUtil.isNullorEmpty(tokenAdmin) && "admin".equals(uriarray[1])) {
            Long loginInfoId = sysUserService.sysUsercheckToken(tokenAdmin);
            if (StringUtil.isNullorEmpty(loginInfoId)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("infoId", loginInfoId);
        }
        //用户app-userInfoId
        if (!StringUtil.isNullorEmpty(tokenApp) && "userApp".equals(uriarray[1])) {
            UsUserLoginInfo userLoginInfo = userInfoService.userCheckToken(tokenApp);
            if (StringUtil.isNullorEmpty(userLoginInfo)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("infoId", userLoginInfo.getUserInfoId());
            this.request.setAttribute("loginId", userLoginInfo.getId());
        }
        //代理商平台-loginInfoId
        if (!StringUtil.isNullorEmpty(tokenAgentAdmin) && "agentAdmin".equals(uriarray[1])) {
            Long loginInfoId = sysAgentInfoService.sysUsercheckToken(tokenAgentAdmin);
            if (StringUtil.isNullorEmpty(loginInfoId)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("infoId", loginInfoId);
        }
        //门店app-storeInfoId
        if (!StringUtil.isNullorEmpty(tokenStoreApp) && "storeApp".equals(uriarray[1])) {
            Long storeInfoId = sysStoreInfoService.storeAppCheckToken(tokenStoreApp);
            if (StringUtil.isNullorEmpty(storeInfoId)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("infoId", storeInfoId);
            //loginId
            Long loginId = sysStoreInfoService.storeAppCheckTokenTologinId(tokenStoreApp);
            if (StringUtil.isNullorEmpty(loginId)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("loginId", loginId);
        }
        //代理商app-loginInfoId
        if (!StringUtil.isNullorEmpty(tokenAgentApp) && "agentApp".equals(uriarray[1])) {
            Long loginInfoId = sysAgentInfoService.sysUsercheckToken(tokenAgentApp);
            if (StringUtil.isNullorEmpty(loginInfoId)) {
                throw new BizException(CodeIdEnum.Expired);
            }
            this.request.setAttribute("infoId", loginInfoId);
        }

        if (StringUtil.isNullorEmpty(this.request.getAttribute("infoId"))) {
            throw new BizException(CodeIdEnum.Expired);
        }


    }

}
